package com.wedo.mall.activity.agent;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wedo.mall.R;
import com.wedo.mall.adapter.GoodsAdapter;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.ptr.Pull2Refresh;
import com.wedo.mall.widget.SearchView;

import butterknife.BindView;

/**
 * 搜索店内商品
 * Created by e on 2017/7/20.
 */

public class AgentSearchGoodsActivity extends EActivity{

    @BindView(R.id.sv_search) SearchView sv_search;
    @BindView(R.id.tv_empty) TextView tv_empty;
    @BindView(R.id.rv_list) XRecyclerView rv_list;
    private Pull2Refresh<Goods> ptr;
    private String id;

    @Override
    protected int layout() {
        return R.layout.activity_agent_search_goods;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        id = getIntent().getStringExtra(AgentDetailActivity.EXTRA_AGENT);
        rv_list.setHasFixedSize(true);
        rv_list.setLayoutManager(new GridLayoutManager(this,2));

        GoodsAdapter adapter = new GoodsAdapter(this,null);
        adapter.listener(this);
        ptr = new Pull2Refresh<>();
        ptr.widget(rv_list).adapter(adapter).url(EApi.AGENT_SEARCH_GOODS).param("id",id).clazz(Goods.class).emptyWidget(tv_empty).build();

        sv_search.listener(new SearchView.SearchListener() {
            @Override
            public void search(String keyword) {
                AgentSearchGoodsActivity.this.search(keyword);
            }
        });
        sv_search.requestPermission(this);
    }

    /**搜索*/
    private void search(String keyword){
        hideKeybord();
        ptr.param("keyword",keyword).refresh();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        sv_search.onRequestPermissionsResult(this,requestCode,permissions,grantResults);
    }
}
